﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;

using System.Linq;
using System.Collections.Generic;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_ContentProperty。
    /// </summary>
    public class ContentPropertyDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ContentProperty model)
        {
            using (Entities db = new Entities())
            {
                db.ContentPropertes.Add(model);
                db.SaveChangesAsync().Wait();

                return model.Id;
            }

        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(ContentProperty model)
        {
            using (Entities db = new Entities())
            {
                db.ContentPropertes.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Id"></param>
        public void Delete(int Id)
        {
            using (Entities db = new Entities())
            {
                var q = db.ContentPropertes.Where(x => x.Id == Id).FirstOrDefault();
                db.ContentPropertes.Remove(q);
                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region 绑定DDL控件
        /// <summary>
        /// 绑定DDL控件
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="iType"></param>
        public IList<ContentProperty> BindDDL(byte modelId, int iType)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.ContentPropertes
                        where x.ModelId == modelId && x.Type == iType
                        select x;

                return q.ToArray();
            }
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="type"></param>
        /// <param name="keyword"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<ContentProperty> BindList(byte modelId, int type, string keyword, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = (from x in db.ContentPropertes
                             where x.ModelId == modelId && x.Type == type
                             select x);
                if (string.IsNullOrEmpty(keyword) == false)
                {
                    query = query.Where(x => x.Name.Contains(keyword));
                }

                pp.RecordCount = query.Count();
                query = query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize);

                return query.ToArray();
            }
        }
        #endregion
    }
}